package com.deyou.activity;

import android.app.usage.UsageEvents;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.deyou.Constants;
import com.deyou.R;
import com.deyou.base.BaseActivity;
import com.deyou.event.LoginEvent;
import com.deyou.util.AndroidUtils;
import com.deyou.util.CacheManager;
import com.deyou.util.SPUtils;
import com.deyou.util.TDeviceUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.iv_left_back)
    ImageView ivLeftBack;
    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_on_off)
    ImageView ivOnOff;
    @BindView(R.id.update_pwd_layout)
    RelativeLayout updatePwdLayout;
    @BindView(R.id.tv_money_type)
    TextView tvMoneyType;
    @BindView(R.id.money_type_layout)
    RelativeLayout moneyTypeLayout;
    @BindView(R.id.tv_cache_size)
    TextView tvCacheSize;
    @BindView(R.id.clear_cache_layout)
    RelativeLayout clearCacheLayout;
    @BindView(R.id.about_us_layout)
    RelativeLayout aboutUsLayout;
    @BindView(R.id.app_comment_layout)
    RelativeLayout appCommentLayout;
    @BindView(R.id.out_login_layout)
    RelativeLayout outLoginLayout;
    @BindView(R.id.activity_setting)
    LinearLayout activitySetting;

    private double catchesize;
    private static final int COMPLED = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected boolean translucentStatusBar() {
        return false;
    }

    private void initView() {
        tvRight.setVisibility(View.INVISIBLE);
        tvMidTitle.setText("设置");
        if (SPUtils.getBooleanByKey(SettingActivity.this, Constants.SP_Push_Status))
            ivOnOff.setBackgroundResource(R.drawable.bg_check_sel);
        else
            ivOnOff.setBackgroundResource(R.drawable.bg_check_nor);
        catchesize = getCaChesize();
        if (catchesize > 0)
            tvCacheSize.setText(catchesize + "M");
        else
            tvCacheSize.setText("0M");
        tvMoneyType.setText(TDeviceUtil.getVersionName());
    }

    @OnClick({R.id.iv_left_back, R.id.iv_on_off, R.id.update_pwd_layout, R.id.money_type_layout,
            R.id.clear_cache_layout, R.id.about_us_layout, R.id.app_comment_layout, R.id.out_login_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.iv_on_off:
                if (!SPUtils.getBooleanByKey(SettingActivity.this, Constants.SP_Push_Status)) {
                    ivOnOff.setBackgroundResource(R.drawable.bg_check_sel);
                    SPUtils.saveBooleanByKey(SettingActivity.this, Constants.SP_Push_Status, true);
                } else {
                    ivOnOff.setBackgroundResource(R.drawable.bg_check_nor);
                    SPUtils.saveBooleanByKey(SettingActivity.this, Constants.SP_Push_Status, false);
                }
                break;
            case R.id.update_pwd_layout:
                AndroidUtils.startNextActivity(this, ForgetPwdActivity.class, null, false);
                break;
            case R.id.money_type_layout:
                break;
            case R.id.clear_cache_layout:
                if (catchesize > 0) {
                    cleanCache();
                    catchesize = getCaChesize();
                    if (catchesize == 0) {
                        handler.sendEmptyMessage(COMPLED);
                    } else {
                        showToast("缓存清除失败");
                    }
                } else {
                    showToast("没有缓存需要清理");
                }

                break;
            case R.id.about_us_layout:
                break;
            case R.id.app_comment_layout:
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException e) {
                    showToast("Couldn't launch the market !");
                }
                break;
            case R.id.out_login_layout:
                SPUtils.saveStringByKey(SettingActivity.this, Constants.UserId, "");
                AndroidUtils.startNextActivity(this, LoginActivity.class, null, true);
                EventBus.getDefault().post(new LoginEvent(false));
                break;
        }
    }

    //获取应用缓存大小
    private double getCaChesize() {
        try {
            double cacheSize = CacheManager.getCacheSize(getApplicationContext().getCacheDir());
            return cacheSize;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //清除应用缓存
    private void cleanCache() {
        CacheManager.cleanInternalCache(getApplicationContext());
        Glide.get(getApplicationContext()).clearMemory();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(getApplicationContext()).clearDiskCache();
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case COMPLED:
                    tvCacheSize.setText(catchesize + "M");
                    showToast("缓存清空成功");
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
